package photogcalc.i18n;

import net.sf.microlog.core.LoggerFactory;
import photogcalc.common.Constants;

/** I18nFactory implements factory design pattern. I18nFactory is
 *  used to create and return wanted translation class.
 *
 * @author Antti Okkonen
 */
public class I18nFactory {

    private static I18n i18n = null;
    
    /** Creates and returns instance of the translation class.
     *
     * @param lang language code
     * @return I18n interface to created instance. English is
     *              the default translation class.
     */
    public static I18n createI18n(String lang) {
        LoggerFactory.getLogger("I18nFactory").debug("Lang / country = "+lang);

        if (Constants.APPLET_MODE == true)
            return new I18n_en();

        int split = lang.indexOf("-");
        if (split<=0)
            split = lang.indexOf("_");
        if (split>0)
            lang = lang.substring(0, split);
        LoggerFactory.getLogger("I18nFactory").debug("Lang = "+lang);

        if ("fi".equals(lang)) {
            return new I18n_fi();
        }
        else if ("en".equals(lang)) {
            return new I18n_en();
        }
        else
            return new I18n_en();
    }

    /** Returns, and creates if necessary, an instance of the
     * translation class.
     *
     * @param lang language code
     */
    public static I18n getI18n(String lang) {
        if (i18n == null)
            i18n = createI18n(lang);

        return i18n;
    }
}
